/**
 * 
 */
package cn.sundat.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

import cn.sundat.model.Company;
import cn.sundat.service.CompanyService;

/**
 * @author XIN
 *
 */
public class AdminCompanyAction extends ActionSupport {

	@Resource(name="companyService")
	CompanyService companyService;
	public String list() throws Exception {
		// TODO Auto-generated method stub
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		List<Company> listCompany = companyService.listCompany();
		writer.print(JSONObject.valueToString(listCompany));
		writer.flush();
		writer.close();
		return NONE;
	}
	public String delOne() throws Exception {
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
		String id = ServletActionContext.getRequest().getParameter("comp_id");
		companyService.delById(Long.valueOf(id));
		writer.print(1);
		}catch (Exception e) {
			// TODO: handle exception
			writer.print(0);
		}
		return NONE;
	}
	public String findByKey() throws Exception {
		String key=ServletActionContext.getRequest().getParameter("key");
		String value=ServletActionContext.getRequest().getParameter("value");
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		writer.print(JSONObject.valueToString(companyService.findCompanyByKey(key,value)));
		return NONE;
	}
	public String auditList() throws Exception{
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		List<Company> listCompany = companyService.auditList();
		writer.print(JSONObject.valueToString(listCompany));
		writer.flush();
		writer.close();
		return NONE;
	}
	public String findById() throws Exception{
		Long id=Long.parseLong(ServletActionContext.getRequest().getParameter("id"));
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		writer.print(JSONObject.valueToString(companyService.findCompanyById(id)));
		writer.flush();
		writer.close();
		return NONE;
	}
	public String regApprove() throws Exception{
		Long id=Long.parseLong(ServletActionContext.getRequest().getParameter("id"));
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
			companyService.regApprove(id);
			writer.print(1);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			writer.print(0);
		}
		writer.flush();
		writer.close();
		return NONE;
	}
	public String ApproveUpdateLevel() throws Exception {
		Long id=Long.parseLong(ServletActionContext.getRequest().getParameter("id"));
		Integer nextLevel=Integer.parseInt(ServletActionContext.getRequest().getParameter("nextLevel"));
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
			companyService.ApproveUpdateLevel(id, nextLevel);
			writer.print(1);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			writer.print(0);
		}
		writer.flush();
		writer.close();
		return NONE;
	}
	public String updateLevelList() throws Exception {
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
			List<Company> updateLevelList = companyService.updateLevelList();
			writer.print(JSONObject.valueToString(updateLevelList));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			writer.print(0);
		}
		writer.flush();
		writer.close();
		return NONE;
	}

}
